package v1

import (
	"context"
	"wxpay/internal/dto"
	"wxpay/internal/service"
	wxpay "wxpay/pkg/api/v1"
)

// 提交分账申请  POST "/api/v1/wxpay/profit-receiver/add"
func (s *Server) ProfitReceiverAdd(ctx context.Context, req *wxpay.ProfitReceiverAddRequest) (*wxpay.OperationResult, error) {
	profitReceiverDTO := dto.ProfitReceiverDTO{
		MchID:        req.MchId,
		AppID:        req.AppId,
		Type:         req.Type,
		Account:      req.Account,
		Name:         req.Name,
		RelationType: req.RelationType,
	}
	if err := service.NewProfitService(s.d).AddReceiver(ctx, &profitReceiverDTO); err != nil {
		return nil, err
	}
	return &wxpay.OperationResult{Message: "SUCCESS"}, nil

}

// 提交分账申请  POST "/api/v1/wxpay/profit-order/submit"
func (s *Server) ProfitOrderSubmit(ctx context.Context, req *wxpay.ProfitOrderRequest) (*wxpay.ProfitOrderResponse, error) {
	receivers := make([]dto.ReceiverInProfitShare, len(req.Receivers))
	for i, r := range req.Receivers {
		receivers[i] = dto.ReceiverInProfitShare{
			Type:         r.Type,
			Account:      r.ReceiverAccount,
			Amount:       r.Amount,
			Description:  r.Description,
			ReceiverName: r.ReceiverName,
		}
	}
	profitDTO := dto.ProfitOrderDTO{
		BrandID:    req.BrandId,
		OutTradeNo: req.OutTradeNo,
		OutOrderNo: req.OutOrderNo,
		Receivers:  receivers,
		Finish:     req.Finish,
	}
	if err := service.NewProfitService(s.d).OrderSubmit(ctx, &profitDTO); err != nil {
		return nil, err
	}
	return &wxpay.ProfitOrderResponse{WxOrderId: profitDTO.WxOrderID}, nil

}

// 提交分账回退请求 POST /api/v1/wxpay/profit-return/submit
func (s *Server) ProfitReturnSubmit(ctx context.Context, request *wxpay.ProfitReturnRequest) (*wxpay.ProfitReturnResponse, error) {
	pftReturnDto := dto.ProfitReturnDTO{
		BrandID:     request.BrandId,
		OutOrderNo:  request.OutOrderNo,
		OutReturnNo: request.OutReturnNo,
		ReturnMchID: request.ReturnMchId,
		Amount:      request.Amount,
		Description: request.Description,
	}
	if err := service.NewProfitService(s.d).ReturnSubmit(ctx, &pftReturnDto); err != nil {
		return nil, err
	}
	return &wxpay.ProfitReturnResponse{WxReturnNo: pftReturnDto.WxReturnNo}, nil
}

// 提交分账完结请求 POST /api/v1/wxpay/profit-finish/submit
func (s *Server) ProfitShareFinishSubmit(ctx context.Context, request *wxpay.ProfitShareFinishRequest) (*wxpay.ProfitShareFinishResponse, error) {
	profitDTO := dto.ProfitOrderDTO{
		BrandID:     request.BrandId,
		OutTradeNo:  request.OutTradeNo,
		OutOrderNo:  request.OutOrderNo,
		Description: request.Description,
	}
	if err := service.NewProfitService(s.d).FinishSubmit(ctx, &profitDTO); err != nil {
		return nil, err
	}
	return &wxpay.ProfitShareFinishResponse{WxOrderId: profitDTO.WxOrderID}, nil
}

// 查询订单剩余待分金额 POST /api/v1/wxpay/trades/{out_trade_no}/un-split-amount
func (s *Server) UnSplitAmountQuery(ctx context.Context, request *wxpay.UnSplitAmountQueryRequest) (*wxpay.UnSplitAmountQueryResponse, error) {
	profitDTO := dto.ProfitOrderDTO{
		BrandID:    request.BrandId,
		OutTradeNo: request.OutTradeNo,
	}

	amount, err := service.NewProfitService(s.d).UnSplitAmountQuery(ctx, &profitDTO)
	if err != nil {
		return nil, err
	}
	return &wxpay.UnSplitAmountQueryResponse{UnsplitAmount: amount.UnSplitAmount}, nil
}
